<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder;
class Course extends Model
{
    use SoftDeletes;

    public $timestamps = false;

    protected $fillable = ['title', 'description', 'content_type', 'content', 'course_type', 'content_type', 'exp', 'depth'];
    protected $casts = ['event' => 'array', 'complete_conditions' => 'array'];
    const CONTENT_TYPE_RICHTEXT = 0;
    const CONTENT_TYPE_VIDEO = 1;
    const COURSE_TYPE_MAIN = 0;
    const COURSE_TYPE_MINOR = 1;
    const COURSE_TYPE_PRACTISE = 2;
    const CONTENT_TYPES = [self::CONTENT_TYPE_RICHTEXT => '富文本', self::CONTENT_TYPE_VIDEO => '视频'];
    const COURSE_TYPES = [self::COURSE_TYPE_MAIN => '主线', self::COURSE_TYPE_MINOR => '支线', self::COURSE_TYPE_PRACTISE => '实践'];

//    protected static function boot()
//    {
//        parent::boot();
//
//        static::addGlobalScope('age', function (Builder $builder) {
//            $builder->orderBy('course_type', 'asc');
//        });
//    }
    public function skills()
    {
        return $this->belongsToMany(Course::class, 'skill_course')->using(SkillCourse::class)->withPivot(SkillCourse::OTHER_COLUMNS);
    }
    public function scopeSortType($query)
    {
        return $query->orderBy('course_type', 'asc');
    }
    public function users()
    {
        return $this->belongsToMany(User::class, 'user_course')->using(UserCourse::class);
    }

    public function works()
    {
        return $this->hasMany(Work::class);
    }

    public function getContentTypeTextAttribute()
    {
        return self::CONTENT_TYPES[$this->content_type] ?? '无类型';
    }

    public function getCourseTypeTextAttribute()
    {
        return self::COURSE_TYPES[$this->course_type] ?? '无类型';
    }
}
